
using System.Drawing;
using System.Collections.Generic;
using System;
using SharpWind.Input;
using Microsoft.DirectX.Direct3D;

namespace SharpWind.Gui.Controls
{
	public class ListBox : TextBoxBase
	{
		private int selectedIndex = 0;

		public event EventHandler SelectedIndexChanged = null;

		public ListBox() : base( "listbox" )
		{
			Size				   = new SizeF( 120, 100 );
			ShowVScrollBar		   = true;
			TextAlign			   = TextAlign.TopLeft;
			this.lines.ItemAdd	  += new EventHandler( OnItemAdd );
			this.lines.ItemRemove += new EventHandler( OnItemRemove );
		}

		private void OnItemAdd( object sender, EventArgs e )
		{
			Items[Items.Count - 1] += '\n';
			this.label.Text += Items[Items.Count - 1];

			int max = this.lines.Count - this.maxLines;
			max = ( max < 0 ) ? 0 : max;
			
			this.vScrollBar.Maximum = max;
			this.vScrollBar.Value	= 0;
			this.vScrollBar.Enabled = max == 0 ? false : true;
		}

		internal override void MouseDown( MouseEventArgs e )
		{
			if( Visible )
			{
				base.MouseDown( e );
				RectangleF rect = new RectangleF( this.label.Location, this.label.Size );
				if( rect.Contains( new PointF( e.AbsX, e.AbsY ) ) )
				{
					SelectedIndex = ( (int)Math.Floor( ( e.AbsY - this.label.Location.Y ) / this.lineHeight ) + this.startLine );
				}
			}
		}

		protected override void OnRender()
		{
			float index = this.selectedIndex - this.startLine;

			if( this.Items.Count > 0 && index >= 0 &&
				index < this.maxLines )
			{
				RenderHighlight();
			}
		}

		private void RenderHighlight()
		{
			Engine.Instance.Renderer.GuiSprite.Begin( SpriteFlags.AlphaBlend );

			Theme theme = Engine.Instance.Scene.Controls.Theme;
			Theme.Control listBox = theme.Controls[this.control];
			Theme.Control.Element element = listBox.Elements[this.curentState.ToString() + "Highlight"];

			float index = this.selectedIndex - this.startLine;

			Engine.Instance.Renderer.GuiSprite.Draw2D( theme.Texture.D3DTexture,
			                                           new Rectangle( element.X, element.Y, element.Width, element.Height ),
			                                           new SizeF( this.label.Size.Width - 1, this.lineHeight - 2 ),
			                                           new PointF( this.label.Location.X + 1,
			                                                       this.label.Location.Y + 2 +
																   ( index * this.lineHeight ) ), element.Color );

			Engine.Instance.Renderer.GuiSprite.End();
		}

		private void OnItemRemove( object sender, EventArgs e )
		{
			this.label.Text = string.Empty;
			foreach( string item in Items )
			{
				this.label.Text += item;
			}

			int max = this.maxLines - this.lines.Count;
			max = ( max < 0 ) ? 0 : max;

			this.vScrollBar.Maximum = max;
			this.vScrollBar.Value	= 0;
			this.vScrollBar.Enabled = max == 0 ? false : true;
		}

		public ObjectCollection<string> Items
		{
			get
			{
				return this.lines;
			}
		}

		public int SelectedIndex
		{
			get
			{
				return this.selectedIndex;
			}
			set
			{
				if( this.selectedIndex != value &&
					value >= 0 && value <= this.Items.Count - 1 )
				{
					this.selectedIndex = value;

					if( SelectedIndexChanged != null )
					{
						SelectedIndexChanged( this, EventArgs.Empty );
					}
				}
			}
		}
	}
}
